package com.shangxia.rachel.common.util.file;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;

/**
 * @ProjectName: rachel
 * @ClassName: CopyFilesExample
 * @Time: 2020/6/11
 * @Author: zy9524
 * @Description:
 * @Experience:
 */

@Slf4j
public class CopyFilesExample {
    public static void main(String[] args) throws InterruptedException,
            IOException {

        File source = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll1.mp4");
        File dest = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll122.mp4");

        // copy file using FileStreams
        long start = System.currentTimeMillis();
        long end;
//        copyFileUsingFileStreams(source, dest);
        System.out.println("Time taken by FileStreams Copy 1= "
                + (System.currentTimeMillis() - start));

        // copy files using java.nio.FileChannel
        source = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll1.mp4");
        dest = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll133.mp4");
        start = System.currentTimeMillis();
//        copyFileUsingFileChannels(source, dest);
        end = System.currentTimeMillis();
        System.out.println("Time taken by FileChannels Copy 2= " + (end - start));

        // copy file using Java 7 Files class
        source = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll1.mp4");
        dest = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll144.mp4");
        start = System.currentTimeMillis();
//        copyFileUsingJava7Files(source, dest);
        end = System.currentTimeMillis();
        System.out.println("Time taken by Java7 Files Copy 3= " + (end - start));

        // copy files using apache commons io
        source = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll1.mp4");
        dest = new File("C:\\Users\\zy9524\\Videos\\Captures\\lzll155.mp4");
        start = System.currentTimeMillis();
//        copyFileUsingApacheCommonsIO(source, dest);
        end = System.currentTimeMillis();
        System.out.println("Time taken by Apache Commons IO Copy 4= "
                + (end - start));

    }

    public static void copyFileUsingFileStreams(File source, File dest) throws IOException {
        try (InputStream input = new FileInputStream(source);
             OutputStream output = new FileOutputStream(dest);) {
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } catch (EOFException e) {

        }
    }

    public static void copyFileUsingFileChannels(File source, File dest) throws IOException {

        try (FileChannel inputChannel = new FileInputStream(source).getChannel();
             FileChannel outputChannel = new FileOutputStream(dest).getChannel();) {
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } catch (EOFException e) {
            log.error("EOFException{}", e);
        }
    }

    public static void copyFileUsingJava7Files(File source, File dest)
            throws IOException {
        Files.copy(source.toPath(), dest.toPath());
    }

    public static void copyFileUsingApacheCommonsIO(File source, File dest)
            throws IOException {
        FileUtils.copyFile(source, dest);
    }
}
